package com.atguigu.serviceedu.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.serviceedu.client.VodClient;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.EduCourseDescription;
import com.atguigu.serviceedu.entity.frontVo.CourseWebVo;
import com.atguigu.serviceedu.entity.frontVo.FrontCourseVo;
import com.atguigu.serviceedu.entity.vo.CourseInfoVo;
import com.atguigu.serviceedu.entity.vo.CoursePublishVo;
import com.atguigu.serviceedu.mapper.EduCourseMapper;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseDescriptionService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.serviceedu.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    //小节，章节，描述，本身
    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    //删除视频
    @Autowired
    private VodClient vodClient;


    //添加课程信息
    @Override
    public String saveCourse(CourseInfoVo courseInfoVo) {
        //1、课程信息放入表中
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        int insert = baseMapper.insert(course);
        if(insert == 0){//插入失败
            throw  new GuliException(20001,"新增课程信息失败");
        }

        String id = course.getId();
        //2、课程简介放入表中
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        //为了保证一对一的关系课程id设置成课程简介的id
        eduCourseDescription.setId(id);
        eduCourseDescriptionService.save(eduCourseDescription);
        return id;
    }

    //根据课程id返回课程信息
    @Override
    public CourseInfoVo findCourseById(String courseId) {
        /**
         * 1、查询课程基本信息
         * 2、查询课程简介
         */
        EduCourse course = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(course,courseInfoVo);

        //查询课程简介
        EduCourseDescription courseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription( courseDescription.getDescription());

        return courseInfoVo;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        int index = baseMapper.updateById(course);
        if(index == 0){
            throw new GuliException(20001,"修改课程信息失败");
        }else {
            EduCourseDescription description = new EduCourseDescription();
            description.setId(courseInfoVo.getId());
            description.setDescription(courseInfoVo.getDescription());
            eduCourseDescriptionService.updateById(description);
        }
    }

    @Override
    public CoursePublishVo getCoursePublishVo(String courseId) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishVoById(courseId);
        return coursePublishVo;
    }

    //删除课程：包括课程的小节，章节，描述，本身
    @Override
    @Transactional
    public void removeCourseById(String courseId) {

        //获取视频id删除阿里云视频
        //t通过课程id查询阿里云视频id
        //1、删除小节
        eduVideoService.deleteVideoByCourseId(courseId);

        //2、章节
        eduChapterService.deleteChapterByCourseId(courseId);
        //3、描述
        eduCourseDescriptionService.removeById(courseId);
        //4、自己
        int result = baseMapper.deleteById(courseId);

        if(result == 0){
            throw new GuliException(20001,"删除课程信息失败");
        }
    }

    @Override
//    @Cacheable(value = "course",key = "'getPageCourse'")
    public Map<String, Object> getPageCourse(Page<EduCourse> pageCourse, FrontCourseVo frontCourseVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //逐个取出条件，判断，如果有值，则拼接sql
        if (!StringUtils.isEmpty(frontCourseVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", frontCourseVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(frontCourseVo.getSubjectId())) {
            wrapper.eq("subject_id", frontCourseVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(frontCourseVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(frontCourseVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(frontCourseVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageCourse, wrapper);
        Map<String,Object> map = new HashMap<>();

        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();

        map.put("records",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("hasNext",hasNext);
        map.put("total",total);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo selectCourseInfo(String courseId) {
        return baseMapper.selectCourseInfo(courseId);
    }
}
